
from flask import Blueprint, redirect,render_template, request, session, url_for
from sqlalchemy import and_
from app.models.base import db
from app.models.Member import Member
from app.models.Medicine import Medicine


MemberBP = Blueprint('Member',__name__)

@MemberBP.route('', methods=['GET','POST'])

def get_Member():
    t_id=session.get('id')
    t_name=session.get('name')
    if request.method == 'GET':
        return render_template('Member.html',title='tudent',header='Sample Case',name=t_name,id=t_id)
    else :
        search_field = request.form.get('search_field', 'CName')
        search_value = request.form.get('search_value', '')

        # 添加调试信息
        print(f'Searching by {search_field} with value {search_value}')

        results = search_items(search_field, search_value)

        # 检查搜索结果
        print(f'Found {len(results)} results')

        return render_template('Member.html', results=results, search_field=search_field, search_value=search_value,name=t_name,id=t_id)

def search_items(search_field, search_value):
    results = []

    if search_field == 'MName':
        medicines = Medicine.query.filter(Medicine.Medicine_Name.like(f'%{search_value}%')).all()
        for medicine in medicines:
                    results.append({
                        'M_id': medicine.M_id,
                        'Medicine_Name': medicine.Medicine_Name,
                        'Medicine_Category': medicine.Medicine_Category,
                        'Price': medicine.Price,
                    })

    elif search_field == 'MCategory':
        medicines = Medicine.query.filter(Medicine.Medicine_Category.like(f'%{search_value}%')).all()
        for medicine in medicines:
                    results.append({
                        'M_id': medicine.M_id,
                        'Medicine_Name': medicine.Medicine_Name,
                        'Medicine_Category': medicine.Medicine_Category,
                        'Price': medicine.Price,
                    })

    elif search_field == 'MNumber':
        medicines = Medicine.query.filter(Medicine.M_id=={search_value}).all()
        for medicine in medicines:
                    results.append({
                        'M_id': medicine.M_id,
                        'Medicine_Name': medicine.Medicine_Name,
                        'Medicine_Category': medicine.Medicine_Category,
                        'Price': medicine.Price,
                    })
    return results
@MemberBP.route('/medicine/<int:a_id>/<string:t_name>/<int:t_id>',methods=['GET','POST'])
def medicine_detail(a_id,t_name,t_id):
    if request.method == 'GET':
        # 通过 A_id 查询对应的答案及其相关信息
        medicine = Medicine.query.get_or_404(a_id)
        if medicine:
            
                return render_template('medicine_detail.html', medicine=medicine, name=t_name,a_id=str(a_id),id=t_id)
            
        return "<h1>Answer not found</h1>", 404
    else:
        if request.method == 'POST' and 'search_button' in request.form:
        #action = request.form.get('search_button')
        #if action == 'search_button':
            search_field = request.form.get('search_field', 'CName')
            search_value = request.form.get('search_value', '')
            results = search_items(search_field, search_value)
            return render_template('Member.html', results=results, search_field=search_field, search_value=search_value,name=t_name,id=t_id)
